/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;
import negocio.Admin;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
 *
 * @author Diego
 */
public class Persistencia {
    private SessionFactory sessionFactory;
    private Session session;
    public Persistencia() {
    sessionFactory = new Configuration().configure().buildSessionFactory();
    session = sessionFactory.openSession();
    }
     public void insert(Object unObjeto) {
    Transaction tr = session.beginTransaction();
    session.save(unObjeto);
    tr.commit();
    }
    public void update(Object unObjeto){
        Transaction tr = session.beginTransaction();
            session.saveOrUpdate(unObjeto);
        tr.commit();
    }
    public void delete(Object unObjeto){
        Transaction tr = session.beginTransaction();
        session.delete(unObjeto);
        tr.commit();
    }
    
    public Admin cargarCargarAdmin(int unId) throws Exception{
        Admin unAdmin = null;
        try{
            unAdmin = (Admin) session.get(Admin.class, unId);
        }
        catch(Exception ex){
            throw new Exception("Error al cargar la empresa: "+ex.getMessage());
        }
        return unAdmin;
    }
}
